////////////////////////////////////////////////////////////////////////////////
// Filename: vector2d.cpp
////////////////////////////////////////////////////////////////////////////////
#include "vector2d.hpp"

Vector2D::Vector2D() {
	x = 0;
	y = 0;
}

Vector2D::Vector2D(float x, float y) {
	this->x = x;
	this->y = y;
}

Vector2D::Vector2D(const Vector2D& other) {
	x = other.x;
	y = other.y;
}

Vector2D::~Vector2D() {

}

void Vector2D::Set(float x, float y) {
	SetX(x);
	SetY(y);
}

void Vector2D::SetX(float x) {
	this->x = x;
}

void Vector2D::SetY(float y) {
	this->y = y;
}

Vector2D Vector2D::Add(Vector2D& a, Vector2D& b) {
	return Vector2D(a.GetX() + b.GetX(), a.GetY() + b.GetY());
}

Vector2D Vector2D::Sub(Vector2D& a, Vector2D& b) {
	return Vector2D(a.GetX() - b.GetX(), a.GetY() - b.GetY());
}

Vector2D Vector2D::Scale(Vector2D& a, float scalar) {
	return Vector2D(a.GetX() * scalar, a.GetY() * scalar);
}

Vector2D Vector2D::Normalize(Vector2D& a) {
	float factor = 1 / sqrtf(ScalarProduct(a));
	return Scale(a, factor);
}

Vector2D Vector2D::Project(Vector2D& a, Vector2D& b) {
	float factor = ScalarProduct(a, b) / ScalarProduct(b);
	return Scale(b, factor);
}

Vector2D Vector2D::Rot(Vector2D* a, float angle) {
	float x = a->x * cosf(angle) - a->y * sinf(angle);
	float y = a->x * sinf(angle) + a->y * cosf(angle);

	return Vector2D(x, y);
}

float Vector2D::Length(Vector2D& a) {
	return sqrtf(ScalarProduct(a));
}

float Vector2D::Distance(Vector2D& a, Vector2D& b) {
	return sqrtf(ScalarProduct(Sub(b, a)));
}

float Vector2D::ScalarProduct(Vector2D& a) {
	return ScalarProduct(a, a);
}

float Vector2D::ScalarProduct(Vector2D& a, Vector2D& b) {
	return a.GetX() * b.GetX() + a.GetY() * b.GetY();
}

float Vector2D::GetX() {
	return x;
}

float Vector2D::GetY() {
	return y;
}

std::ostream& operator<<(std::ostream& strm, const Vector2D& v) {
	return strm << "Vector2D(" << v.x << ", " << v.y << ")";
}
